# coding=utf-8
import os

if __name__ == '__main__':
    template = """
{
    "job": {
        "content": [{
            "writer": {
                "parameter": {
                    "username": "portal",
                    "loadUrl": ["10.104.7.41:8030", "10.104.7.145:8030", "10.104.7.146:8030", "10.104.7.55:8030", "10.104.7.131:8030"],
                    "postSql": [],
                    "database": "cache_log",
                    "column": ["domain", "time_at", "isp", "province", "stream", "counts"],
                    "table": "statis_uniqueip_daily",
                    "password": "portal@Baishancloud#1qaz",
                    "preSql": [],
                    "jdbcUrl": "",
                    "loadProps": {
                        "column_separator": ","
                    }
                },
                "name": "doriswriter"
            },
            "reader": {
                "parameter": {
                    "username": "master",
                    "connection": [{
                        "querySql": [
                            "%s"
                        ],
                        "jdbcUrl": ["jdbc:mysql://10.104.3.2:3306/cache_log"]
                    }],
                    "password": "master@Baishan#1qaz"
                },
                "name": "mysqlreader"
            }
        }],
        "setting": {
            "speed": {
                "channel": 1
            },
            "errorLimit": {
                "record": 0,
                "percentage": 0
            }
        }
    }
}
    """
    begin = 1617206400
    end = 1635609600
    while begin <= end:
        sql = "select domain, from_unixtime(time_at, '%%Y-%%m-%%d 00:00:00') as time_at, isp, province, stream, counts from statis_uniqueip_daily where time_at = %d " % end
        job = template % sql
        with open("job/statis_uniqueip_daily-doris/%d.job" % end, mode="w") as f:
            print >> f, job
        res = os.system("python bin/datax.py job/statis_uniqueip_daily-doris/%d.job" % end)
        print(res)
        end -= 86400
